package com.warrior.service;

import com.warrior.entity.ActivityEntity;
import com.warrior.entity.TicketEntity;
import com.warrior.respository.ITicketRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Shawn on 2017/3/15.
 */
@Service
public class TicketService {

    private static final Logger logger = LogManager.getLogger(TicketService.class.getName());

    @Autowired
    private ITicketRepository iTicketRepository;

    @Autowired
    private ActivityService activityService;

    /**
     * 添加一个新的票务
     *
     * @param ticketEntity
     */
    @Transactional
    public void addNewTicket(TicketEntity ticketEntity) {
        if (logger.isDebugEnabled()) {
            logger.debug("addNewTicket(TicketEntity ticketEntity={}) - start", ticketEntity); //$NON-NLS-1$
        }

        this.iTicketRepository.save(ticketEntity);

        if (logger.isDebugEnabled()) {
            logger.debug("addNewTicket(TicketEntity) - end"); //$NON-NLS-1$
        }
    }

    /**
     * 根据id查找票务
     *
     * @param id 票务id
     * @return TicketEntity
     */
    @Transactional(readOnly = true)
    public TicketEntity findTicketById(String id) {
        if (logger.isDebugEnabled()) {
            logger.debug("findTicketById(String id={}) - start", id); //$NON-NLS-1$
        }

        TicketEntity returnTicketEntity = this.iTicketRepository.findTicketEntityById(id);
        if (logger.isDebugEnabled()) {
            logger.debug("findTicketById(String) - end"); //$NON-NLS-1$
        }
        return returnTicketEntity;
    }

    /**
     * 根据id和状态查找
     *
     * @param id     票务id
     * @param status 票务状态
     * @return TicketEntity
     */
    @Transactional(readOnly = true)
    public TicketEntity findTicketByIdAndStatus(String id, int status) {
        if (logger.isDebugEnabled()) {
            logger.debug("findTicketByIdAndStatus(String id={}, int status={}) - start", id, status); //$NON-NLS-1$
        }

        TicketEntity returnTicketEntity = this.iTicketRepository.findTicketEntityByIdAndTicketStatus(id, status);
        if (logger.isDebugEnabled()) {
            logger.debug("findTicketByIdAndStatus(String, int) - end"); //$NON-NLS-1$
        }
        return returnTicketEntity;
    }


    /**
     * @param page      页码
     * @param size      码数
     * @param orderType 排序类型
     * @return Page<TicketEntity>
     */
    @Transactional
    public Page<TicketEntity> findAllTicketInPage(int page, int size, String orderType) {
        if (logger.isDebugEnabled()) {
            logger.debug("findAllTicketInPage(int page={}, int size={}, String orderType={}) - start", page, size, orderType); //$NON-NLS-1$
        }

        Sort sort = new Sort(Sort.Direction.ASC, "createdAt");
        if ("desc".equals(orderType)) {
            sort = new Sort(Sort.Direction.DESC, "createdAt");
        }
        //设置分页参数
        Pageable pageable = new PageRequest(page, (size <= 0) ? 10 : size, sort);
        Page<TicketEntity> ticketEntityByIdAndTicketStatus = this.iTicketRepository.findByTicketStatus(1, pageable);

        if (logger.isDebugEnabled()) {
            logger.debug("findAllTicketInPage(int, int, String) - end"); //$NON-NLS-1$
        }
        return ticketEntityByIdAndTicketStatus;
    }

    @Transactional
    public void deletedTicket(String ticketId){
        this.iTicketRepository.delete(ticketId);
    }

    @Transactional
    public List<TicketEntity> findTicketByActivityId(String activityId){
        ActivityEntity activityEntity = this.activityService.findActivityById(activityId);
        return this.iTicketRepository.findTicketEntitiesByactivityByActivityId(activityEntity);
    }

}
